﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Linq;
using System.Text;
using System.Data.Sql;
using System.Windows.Forms;



namespace DAO
{

     
    public class DataProvider
    {
       

   
          
        public static  SqlConnection con;
    //   public static SqlDataAdapter da;
        static DataSet ds;
        public static SqlCommand cmd;
       
       
        public static SqlConnection TaoKetNoi()
        {
           
                          
            if ((con != null) && (con.State == ConnectionState.Open))
            {
                con.Close();
            }
            try
            {
                con = new SqlConnection();
                con.ConnectionString = "Data Source=.;Initial Catalog=QLBanHang;Integrated Security=True";

                
        
                return con;
            }
            catch (Exception ex)
            {
               
                con = null;
                return con;
            }

           
        }

        public static DataTable LayDataTable(string sQuery, SqlConnection con)
        {
            try
            {
                DataTable dt = new DataTable();

                SqlDataAdapter da = new SqlDataAdapter(sQuery, con);
                con.Open();
                da.Fill(dt);
                con.Close();
                return dt;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        
     

        public static bool ExecuteNonQuery(string sQuery, SqlConnection con)
        {


           
            try
            {
                SqlCommand cmd = new SqlCommand(sQuery, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                return true;
            }
            catch (Exception ex)
            {

                return false;
            }
        }
        public static void DongKetNoi(SqlConnection con)
        {
            con.Close();
        }

        /*  public  SqlConnection TaoKetNoi()
    {
     
      
       try
        {
            
            SqlConnection con = new SqlConnection(SqlConnectionString);
            con.Open();
            return con;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
    DataSet =
     private DataTable ;  
    public static DataTable LayDataTable(string sQuery, SqlConnection con)
    {
        try
        {
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sQuery, SqlConnection con);
            da.Fill(dt);
            return dt;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
    public static bool ExecuteNonQuery(string sQuery, SqlConnection con)
    {
        try
        {
            SqlCommand cmd = new SqlCommand(sQuery, con);
            cmd.ExecuteNonQuery();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static void DongKetNoi(SqlConnection con)
    {
        con.Close();
    }*/

        //internal static DataTable LayDataTable(string sQuery, SqlConnection con)
        //{
        //    throw new NotImplementedException();
        //}

        //internal static DataTable LayDataTable(string sQuery, SqlConnection con)
        //{
        //    throw new NotImplementedException();
        //}

        
    }
}